package com.q.tqb.view.dialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Point;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.activity.login.AgrementActivity;
import com.q.tqb.activity.login.LoginActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseApplication;
import com.q.tqb.util.DensityUtil;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class LoginDialog extends Dialog {

    private CheckBox cbAgreement;

    private Activity mActivity;
    public IWXAPI mWxApi;

    public LoginDialog(Activity activity) {
        super(activity, R.style.myDialog);
        this.mActivity = activity;
        initLoginDialog();
    }

    private void initLoginDialog() {

        View v = View.inflate(mActivity, R.layout.dialog_login, null);
        TextView tvAgreement = v.findViewById(R.id.tv_agreement);
        TextView tvWxLogin = v.findViewById(R.id.tv_wx_login);
        TextView tvTelLogin = v.findViewById(R.id.tv_tel_login);
        cbAgreement = v.findViewById(R.id.cb_agreement);
        tvWxLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cbAgreement.isChecked()) {

                    mWxApi = WXAPIFactory.createWXAPI(mActivity, AppConst.WEIXIN_APP_ID, false);
                    // 将该app注册到微信
                    mWxApi.registerApp(AppConst.WEIXIN_APP_ID);

                    if (!mWxApi.isWXAppInstalled()) {
                        Toast.makeText(mActivity, "您还未安装微信客户端", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    final SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "diandi_wx_login";
                    mWxApi.sendReq(req);
                    //     customProgressDialog.show();
                } else {
                    Toast.makeText(mActivity, "请先同意淘趣包协议", Toast.LENGTH_SHORT).show();
                }
            }
        });

        tvTelLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, LoginActivity.class);
                mActivity.startActivityForResult(intent, 51);
            }
        });
        tvAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mActivity, HtmlActivity.class);
                intent.putExtra("title", "用户协议");
                intent.putExtra("url", "http://m.tqboo.com/home/help/xieyi");
                mActivity.startActivity(intent);
            }
        });

        Point size = new Point();
        mActivity.getWindowManager().getDefaultDisplay().getSize(size);

        int height = DensityUtil.dip2px(mActivity, 330);
        setContentView(v);
        Window dialogWindow = getWindow();
        setCancelable(false);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        lp.height = height;
        lp.width = (int) (size.x * 0.70);
        dialogWindow.setAttributes(lp);

    }


}
